{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [],
   "source": [
    "import urllib.request\n",
    "import xlwt\n",
    "from bs4 import BeautifulSoup\n",
    "list1=[]\n",
    "for page in range(1,26):\n",
    "    html=urllib.request.urlopen('https://www.qidian.com/rank/yuepiao?chn=-1&page='+str(page)).read()\n",
    "    bs=BeautifulSoup(html,'html.parser')\n",
    "    for k in bs.select('.rank-view-list a'):\n",
    "        list1.append(k.get_text())\n",
    "    for i in range(len(list1)-1,-1,-1):\n",
    "        if list1[i]=='' or list1[i]=='书籍详情' or list1[i]=='加入书架':\n",
    "            list1.pop(i)\n",
    "workbook=xlwt.Workbook()\n",
    "sheet=workbook.add_sheet('起点TOP500')\n",
    "j=0\n",
    "for i in range(0,len(list1[0:len(list1):4])):\n",
    "    shuming=list1[0:len(list1):4]\n",
    "    zuozhe=list1[1:len(list1):4]\n",
    "    leixing=list1[2:len(list1):4]\n",
    "    gengxin=list1[3:len(list1):4]\n",
    "    shuming.insert(0,'书名')\n",
    "    zuozhe.insert(0,'作者')\n",
    "    leixing.insert(0,'类型')\n",
    "    gengxin.insert(0,'更新')\n",
    "    sheet.write(i,j,shuming[i])\n",
    "    sheet.write(i,j+1,zuozhe[i])\n",
    "    sheet.write(i,j+2,leixing[i])\n",
    "    sheet.write(i,j+3,gengxin[i])\n",
    "workbook.save('D:/起点.xls')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
